﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web;

public partial class UI_view_MyTrails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                fillControl();
            }
            catch (Exception ex) { }
        }
    }
    public void fillControl()
    {
        int uid = 0;
        if (Request["UserId"] != null)
        {
            int.TryParse(Request["UserId"], out uid);
        }
        else
        {
            uid = AppUser.currUser();
        }
        if (uid > 0)
        {
            IList<Bintje.Trail> trails = Bintje.Trail.Load(Session["Session"] as Bintje.bintje.imageoSession);
            if (uid == AppUser.currUser())
            {
                ltrHeading.Text = "My Trails";
            }

            var myTrails = (from trl in trails
                            orderby trl.Id
                            select new
                            {
                                ID = trl.Id,
                                Name = trl.Title,
                                LocationLatitude = trl.Latitude,
                                LocationLongitude = trl.Longitude,
                                LocationCountry = trl.Waypoints != null && trl.Waypoints.Count > 0 ? trl.Waypoints[0].Country : string.Empty,
                                LocationCity = trl.Waypoints != null && trl.Waypoints.Count > 0 ? trl.Waypoints[0].City : string.Empty,
                                TrailDate = trl.TrailCreated,
                                Category = string.Empty,
                                Length = string.Empty,
                                //ImageUrl = trl.FirstWaypoint != null && trl.FirstWaypoint.Images != null && trl.FirstWaypoint.Images[0] != null && trl.FirstWaypoint.Images.Count > 0 && !string.IsNullOrEmpty(trl.FirstWaypoint.Images[0].Url) ? trl.FirstWaypoint.Images[0].Url : "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50"
                                ImageUrl = string.IsNullOrEmpty(trl.ImageUrl) ? "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50" : trl.ImageUrl
                            }).ToArray();
            if (myTrails.Length > 0)
            {
                rptrMyTrails.DataSource = myTrails;
                rptrMyTrails.DataBind();
                lbltrails.Visible = false;
            }
            else
            {
                lbltrails.Text = "no Trails found";
                lbltrails.Visible = true;
                rptrMyTrails.Visible = false;
            }
        }
    }
    
}
